<?php

namespace App\HttpController;

use EasySwoole\Component\Context\ContextManager;
use EasySwoole\HttpAnnotation\AnnotationTag\Context;
use EasySwoole\HttpAnnotation\AnnotationTag\Di;
use EasySwoole\HttpAnnotation\AnnotationTag\Param;

class Index extends Common
{
    /**
     * @Di(key="IOC")
     */
    protected $ioc;

    /**
     * @Context(key="context")
     */
    protected $context;

    /**
     * @Param(name="account",from={GET,POST},notEmpty="不能为空")
     * @Param(name="session",notEmpty="不能为空")
     */
    function index()
    {
        $this->writeJson(200,null,'hello '.$this->request()->getRequestParam('account'));
    }

    /**
     * 
     */
    function home()
    {
        $redisConfig = [
            'host'          => '192.168.0.250',
            'port'          => '6379',
            'auth'          => '',
            'serialize'     => \EasySwoole\Redis\Config\RedisConfig::SERIALIZE_NONE,
        ];
        $redis = new \EasySwoole\Redis\Redis(new \EasySwoole\Redis\Config\RedisConfig($redisConfig));

        $result = $redis->lPop('task:list:100');
        if ($result) $this->writeJson(200, $result);
        if (!$redis->lLen('task:list:100')) {
            for ($i=0; $i < 20; $i++) { 
                $redis->lPush('task:list:100', 1);
            }
        }
    }

    /**
     * @Param(name="account",from={GET,POST},notEmpty="不能为空")
     * @Param(name="session",from={COOKIE},notEmpty="不能为空")
     */
    function index2($account,$session)
    {
        $this->writeJson(200,null,'hello '.$account);
    }

    function ioc()
    {
        $this->writeJson(200,null,"ioc ".$this->ioc);
    }

    function context()
    {
        $this->writeJson(200,null,"context ".$this->context);
    }

    

    public function circuitBreakerFail()
    {
        $this->writeJson(200,null,'this is fail call');
    }

    protected function onRequest(?string $action): ?bool
    {
        ContextManager::getInstance()->set('context',time());
        return  true;
    }

}